import address,{buliding,floor,classroom}from "../../utils/classrooms"
var date = new Date();
var currentHours = date.getHours();
var currentMinute = date.getMinutes();



Page({
  /**
   * 页面的初始数据
   */
  data: {
    newWidth:"",
    newHeight:"",
    // 地址联动是否显示
    isBlock:false,
    // 时间联动是否显示
    isTimeBlock:false,
    // 幢-楼层-教室号
    building:[],
    floor:[],
    classroom:[],
    // 地址联动的value值
    value:[0,0,0],
    textarea:'',

    // startDate用来存储选择的预约时间
    startDate: "请选择预约时间，方便上门服务",
    multiArray: [[[]],[[]],[[]]],
    multiIndex: [0, 0, 0],

    // 接收传过来的要上传的图片的信息
    picsrc: [],

    // 接收用户输入的故障描述内容
    describe_fix_desc:"",
    useropenid:'',
  },

  // 获取用户输入的故障描述输入框的内容
  describe_fix:function(e){
    // console.log(e.detail.value);
    this.setData({
      describe_fix_desc:e.detail.value
    })
  },

  // 获取用户添加的图片的临时缓存地址
  getpicsrc:function(e){
    var getpicsrc = [];
    getpicsrc.push(e)
    this.setData({
      picsrc:getpicsrc[0]
    })
  },

  // 地区信息 输入框聚焦时显示地址联动
  isShowSite:function(){
    this.setData({
      isBlock:true
    })
  },

  // 点击取消和背景隐藏地址联动
  isHideSite:function(){
    this.setData({
      isBlock:false
    })
  },

  // 输入框聚焦时显示时间联动
  isShowTime:function(){
    this.setData({
      isTimeBlock:true
    })
  },

  // 点击取消和背景隐藏时间联动
  isHideTime:function(){
    this.setData({
      isTimeBlock:false
    })
  },

  // 点击确定获取地址信息
  getSiteData:function(){
    var value = this.data.value
    var building = this.data.building[value[0]].name
    var floor = this.data.floor[value[1]].name
    var classroom = this.data.classroom[value[2]].name
 
    this.setData({
      isBlock:false,
      textarea:building+' '+floor+' '+classroom
    })
  },

   // 地址联动
   cityChange:function(event){
    // console.log(event.detail.value)
    var value = event.detail.value
    var buildingIndex = value[0]
    var floorIndex = value[1]
    var classroomIndex = value[2]
    if(buildingIndex != this.data.value[0]){
      var id1 = address.building[buildingIndex].id
      // console.log(id1)
      this.setData({
        value:[buildingIndex,0,0],
        floor:address.floor[id1],
        classroom:address.classroom[address.floor[id1][0].id]
      })
    }else if(floorIndex != this.data.value[1]){
      var id2 = this.data.floor[floorIndex].id
      // console.log(id2)
      this.setData({
        value:[buildingIndex,floorIndex,0],
        classroom:address.classroom[id2]
      })
    }else{
      this.setData({
        value:[buildingIndex,floorIndex,classroomIndex]
      })
    }
    
  },

  // 自动调整图片大小
  autoImage(event){
    var that = this;
    // 获取图片宽高信息
    var imageWidth = event.detail.width;
    var imageHeight = event.detail.height;
    // console.log("图片高度：",imageWidth);
    // console.log("图片宽度：",imageHeight);
    // 获取屏幕宽度和高度信息
    var winWidth = wx.getSystemInfoSync().windowWidth;
    var winHeight = wx.getSystemInfoSync().windowHeight;
    // console.log("屏幕高度：",winWidth);
    // console.log("屏幕宽度：",winHeight);
    // 传入云函数，使图片宽高适应设备
    wx.cloud.callFunction({
      name:"adaptImage",
      data:{
        imageWidth,
        imageHeight,
        winWidth,
        winHeight,
      },
    }).then(res=>{
      // console.log(res.result);
      this.setData({
        newWidth:res.result.newWidth,
        newHeight:res.result.newHeight,
      })
    })
  },
  
  // 点击时间选择器
  pickerTap:function() {
    date = new Date();
    var monthDay = ['今天','明天'];
    var hours = [];
    var minute = [];
    currentHours = date.getHours();
    currentMinute = date.getMinutes();
    // 月-日
    for (var i = 2; i <= 28; i++) {
      var date1 = new Date(date);
      date1.setDate(date.getDate() + i);
      var md = (date1.getMonth() + 1) + "月" + date1.getDate() + "日";
      monthDay.push(md);
    }
    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    };
    if(data.multiIndex[0] === 0) {
      if(data.multiIndex[1] === 0) {
        this.loadData(hours, minute);
      } else {
        this.loadMinute(hours, minute);
      }
    } else {
      this.loadHoursMinute(hours, minute);
    }
    data.multiArray[0] = monthDay;
    data.multiArray[1] = hours;
    data.multiArray[2] = minute;
    this.setData(data);
  },

  // 当多列选择器的列发生改变的时候
  bindMultiPickerColumnChange:function(e) {
    date = new Date();
    var that = this;
    var monthDay = ['今天', '明天'];
    var hours = [];
    var minute = [];
    currentHours = date.getHours();
    currentMinute = date.getMinutes();
    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    };
    // 把选择的对应值赋值给 multiIndex
    data.multiIndex[e.detail.column] = e.detail.value;
    // 然后再判断当前改变的是哪一列,如果是第1列改变
    if (e.detail.column === 0) {
      // 如果第一列滚动到第一行
      if (e.detail.value === 0) {
        that.loadData(hours, minute);
      } else {
        that.loadHoursMinute(hours, minute);
      }
      data.multiIndex[1] = 0;
      data.multiIndex[2] = 0;
      // 如果是第2列改变
    } else if (e.detail.column === 1) {
      // 如果第一列为今天
      if (data.multiIndex[0] === 0) {
        if (e.detail.value === 0) {
          that.loadData(hours, minute);
        } else {
          that.loadMinute(hours, minute);
        }
        // 第一列不为今天
      } else {
        that.loadHoursMinute(hours, minute);
      }
      data.multiIndex[2] = 0;
      // 如果是第3列改变
    } else {
      // 如果第一列为'今天'
      if (data.multiIndex[0] === 0) {
        // 如果第一列为 '今天'并且第二列为当前时间
        if(data.multiIndex[1] === 0) {
          that.loadData(hours, minute);
        } else {
          that.loadMinute(hours, minute);
        }
      } else {
        that.loadHoursMinute(hours, minute);
      }
    }
    data.multiArray[1] = hours;
    data.multiArray[2] = minute;
    this.setData(data);
  },

  // value改变时出发change事件
  bindStartMultiPickerChange: function (e) {
    var that = this;
    var monthDay = that.data.multiArray[0][e.detail.value[0]];
    var hours = that.data.multiArray[1][e.detail.value[1]];
    var minute = that.data.multiArray[2][e.detail.value[2]];
    if (monthDay === "今天") {
      var month = date.getMonth()+1;
      var day = date.getDate();
      monthDay = month + "月" + day + "日";
    } else if (monthDay === "明天") {
      var date1 = new Date(date);
      date1.setDate(date.getDate() + 1);
      monthDay = (date1.getMonth() + 1) + "月" + date1.getDate() + "日";
    } else {
      var month = monthDay.split("-")[0]; // 返回月
      var day = monthDay.split("-")[1]; // 返回日
      // 这一行被我注释了
      // monthDay = month + "月" + day + "日";
    }
    var startDate = monthDay + " " + hours + ":" + minute;
    that.setData({
      startDate: startDate
    })
  },

  loadData: function (hours, minute) {
    var minuteIndex;
    if (currentMinute > 0 && currentMinute <= 10) {
      minuteIndex = 10;
    } else if (currentMinute > 10 && currentMinute <= 20) {
      minuteIndex = 20;
    } else if (currentMinute > 20 && currentMinute <= 30) {
      minuteIndex = 30;
    } else if (currentMinute > 30 && currentMinute <= 40) {
      minuteIndex = 40;
    } else if (currentMinute > 40 && currentMinute <= 50) {
      minuteIndex = 50;
    } else {
      minuteIndex = 60;
    }
    if (minuteIndex == 60) {
      // 时
      for (var i = currentHours + 1; i < 24; i++) {
        hours.push(i);
      }
      // 分
      for (var i = 0; i < 60; i += 10) {
        minute.push(i);
      }
    } else {
      // 时
      for (var i = currentHours; i < 24; i++) {
        hours.push(i);
      }
      // 分
      for (var i = minuteIndex; i < 60; i += 10) {
        minute.push(i);
      }
    }
  },

  loadHoursMinute: function (hours, minute){
    // 时
    for (var i = 8; i < 21; i++) {
      hours.push(i);
    }
    // 分
    for (var i = 0; i < 60; i += 10) {
      minute.push(i);
    }
  },

  loadMinute: function (hours, minute) {
    var minuteIndex;
    if (currentMinute > 0 && currentMinute <= 10) {
      minuteIndex = 10;
    } else if (currentMinute > 10 && currentMinute <= 20) {
      minuteIndex = 20;
    } else if (currentMinute > 20 && currentMinute <= 30) {
      minuteIndex = 30;
    } else if (currentMinute > 30 && currentMinute <= 40) {
      minuteIndex = 40;
    } else if (currentMinute > 40 && currentMinute <= 50) {
      minuteIndex = 50;
    } else {
      minuteIndex = 60;
    }

    if (minuteIndex == 60) {
      // 时
      for (var i = currentHours + 1; i < 24; i++) {
        hours.push(i);
      }
    } else {
      // 时
      for (var i = currentHours; i < 24; i++) {
        hours.push(i);
      }
    }
    // 分
    for (var i = 0; i < 60; i += 10) {
      minute.push(i);
    }
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      // 设置省
      building:address.building,
      // 设置市
      floor:address.floor[address.building[0].id],
      // 设置区
      classroom:address.classroom[address.floor[address.building[0].id][0].id]
    })
    wx.getStorage({
      key: 'nowlogin_user',
    }).then(res=>{
      this.setData({
        useropenid:res.data
      })
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})